﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightCoverage.RepRef;
using System.Windows.Browser;

namespace SilverlightCoverage
{
    public partial class TagCloud : UserControl
    {
        System.Collections.ObjectModel.ObservableCollection<ClassInfo> classInfoCollection;
        IsimpleWcfClient proxy = new IsimpleWcfClient();
        string moduleName = string.Empty;
        string namespaceName = string.Empty;
        string key = string.Empty;
        string value = string.Empty;
        string rptFile = string.Empty;
        int maxTotalLines = 0;
        public TagCloud()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(TagCloud_Loaded);
        }

        void TagCloud_Loaded(object sender, RoutedEventArgs e)
        {
            //Added by Yuvi

            
            IDictionary<string, string> qString = HtmlPage.Document.QueryString;
            foreach (KeyValuePair<string, string> kvp in qString)
            {
                key = kvp.Key;
                value = kvp.Value;
                if (key == "mod1" || key == "modname")
                    moduleName = value;
                if (key == "nName")
                    namespaceName = value;
                if (key == "rptLink")
                    rptFile = value;
                    txtTagCaption.Tag = rptFile; 



            }
            if (moduleName != string.Empty && namespaceName != string.Empty)
            {
                txtTagCaption.Text = moduleName + "-" + namespaceName;
 
            }
            else if (moduleName != string.Empty && namespaceName == string.Empty)
            {
                txtTagCaption.Text = moduleName;
            }
            else 
            {
               

            }
            //Added by Yuvi

            proxy.GetClassInfoAppLevelSLCompleted += new EventHandler<GetClassInfoAppLevelSLCompletedEventArgs>(proxy_GetClassInfoAppLevelSLCompleted);
            proxy.GetClassInfoAppLevelSLAsync(moduleName, namespaceName, rptFile);

        }



        void proxy_GetClassInfoAppLevelSLCompleted(object sender, GetClassInfoAppLevelSLCompletedEventArgs e)
        {
            classInfoCollection = e.Result;
            IEnumerable<TextBlock> textBlocks = (from ClassInfo in classInfoCollection
                                                 select ConvertToTextBlock(ClassInfo));

            var query = from p in classInfoCollection
                        select p.TotalLines;
            maxTotalLines = (int)query.Max();

            Wraproot.Children.Clear();
            foreach (TextBlock textBlock in textBlocks)
            {
                TextBlock txb = new TextBlock();
                txb.Width = 4;
                Wraproot.Children.Add(textBlock);
                Wraproot.Children.Add(txb);
            }
        }

        public TextBlock ConvertToTextBlock(ClassInfo clsItem)
        {


            TextBlock txtBlock = new TextBlock();
            txtBlock.VerticalAlignment = VerticalAlignment.Center;
            if (clsItem.Classname.Length > 10)
            {
                txtBlock.Text = clsItem.Classname.Substring(0, 10) + "...";
            }
            else
            {
                //txtBlock.Text = clsItem.Classname + "," + clsItem.NamespaceName + "," + clsItem.ModuleName;
                txtBlock.Text = clsItem.Classname;

            }

            //txtBlock.Name = clsItem.Classname;
            txtBlock.MouseLeftButtonUp += new MouseButtonEventHandler(txtBlock_MouseLeftButtonUp);
            txtBlock.Foreground = GetColorByCovPercentage(clsItem);



            //if (clsItem.CovPercentage>80)
            //{
            //    txtBlock.FontSize = 37;
            //    //txtBlock.Foreground = new SolidColorBrush(Colors.Green);


            //}
            //else if (clsItem.CovPercentage > 70)
            //{
            //    txtBlock.FontSize = 33;
            //    //txtBlock.Foreground = new SolidColorBrush(Colors.Green);
            //    txtBlock.Foreground = GetColorByCovPercentage(clsItem);


            //}
            //else if (clsItem.CovPercentage > 60)
            //{
            //    txtBlock.FontSize = 30;
            //    //txtBlock.Foreground = new SolidColorBrush(Colors.Blue);
            //    txtBlock.Foreground = GetColorByCovPercentage(clsItem);


            //}
            //else if (clsItem.CovPercentage > 50)
            //{
            //    txtBlock.FontSize = 27;
            //    //txtBlock.Foreground = new SolidColorBrush(Colors.Blue);
            //    txtBlock.Foreground = GetColorByCovPercentage(clsItem);

            //}
            //else if (clsItem.CovPercentage > 40)
            //{
            //    txtBlock.FontSize = 24;
            //    //txtBlock.Foreground = new SolidColorBrush(Colors.Cyan);
            //    txtBlock.Foreground = GetColorByCovPercentage(clsItem);


            //}
            //else if (clsItem.CovPercentage > 30)
            //{
            //    txtBlock.FontSize = 21;
            //    //txtBlock.Foreground = new SolidColorBrush(Colors.Red);
            //    txtBlock.Foreground = GetColorByCovPercentage(clsItem);


            //}
            //else if (clsItem.CovPercentage > 20)
            //{
            //    txtBlock.FontSize = 18;
            //    //txtBlock.Foreground = new SolidColorBrush(Colors.Red);
            //    txtBlock.Foreground = GetColorByCovPercentage(clsItem);


            //}
            //else if (clsItem.CovPercentage > 10)
            //{
            //    txtBlock.FontSize = 15;
            //    //txtBlock.Foreground = new SolidColorBrush(Colors.Red);
            //    txtBlock.Foreground = GetColorByCovPercentage(clsItem);


            //}
            //else
            //{
            //    txtBlock.FontSize = 12;
            //    //txtBlock.Foreground = new SolidColorBrush(Colors.Red);
            //    txtBlock.Foreground = GetColorByCovPercentage(clsItem);

            //}



            if (clsItem.TotalLines > ((maxTotalLines / 100) * 75))
            {
                txtBlock.FontSize = 30;
            }
            else if (clsItem.TotalLines > ((maxTotalLines / 100) * 55))
            {
                txtBlock.FontSize = 25;
            }

            else if (clsItem.TotalLines > ((maxTotalLines / 100) * 25))
            {
                txtBlock.FontSize = 20;
            }
            else
            {
                txtBlock.FontSize = 12;
            }



            LinearGradientBrush br = new LinearGradientBrush();
            br.StartPoint = new Point(0, 0.5);
            br.EndPoint = new Point(1, 0.5);
            GradientStop gs = new GradientStop();
            gs.Color = Colors.Green;
            gs.Offset = 0.0;
            GradientStop gs1 = new GradientStop();
            if (clsItem.CovPercentage > 90)
            {
                gs1.Color = Colors.Green;
                gs1.Offset = clsItem.CovPercentage / 100;
            }
            else
            {
                gs1.Color = Colors.Red;
                gs1.Offset = clsItem.CovPercentage / 100;
            }

            GradientStopCollection gscollection = new GradientStopCollection();
            gscollection.Add(gs);
            gscollection.Add(gs1);
            br.GradientStops = gscollection;

            TextBox tb = new TextBox();
            if (clsItem.Classname.Length > 15)
            {
                tb.Width = clsItem.Classname.Length * 6.6;
            }
            else
            {
                tb.Width = 100;
            }
            tb.Height = 25;
            tb.Background = br;
            tb.Text = clsItem.Classname;
            tb.Foreground = new SolidColorBrush(Colors.White);

            StackPanel sp = new StackPanel();
            //sp.Background = new SolidColorBrush(Colors.LightGray);
            sp.Background = GetColorByCovPercentage(clsItem);
            sp.Orientation = Orientation.Vertical;
            //sp.Children.Add(tb);

            TextBlock txtClsCap = new TextBlock();
            txtClsCap.Text = "Class: " + clsItem.Classname;
            sp.Children.Add(txtClsCap);

            TextBlock tbcov = new TextBlock();
            tbcov.Text = "Coverage Percentage : " + Math.Round(clsItem.CovPercentage, 2).ToString();
            sp.Children.Add(tbcov);
            ToolTipService.SetToolTip(txtBlock, sp);

            TextBlock tbmod = new TextBlock();
            tbmod.Text = "Module Name : " + clsItem.ModuleName;
            sp.Children.Add(tbmod);

            TextBlock tbns = new TextBlock();
            tbns.Text = "NameSpace : " + clsItem.NamespaceName;
            sp.Children.Add(tbns);


            ToolTipService.SetToolTip(txtBlock, sp);


            txtBlock.Tag = clsItem;
            return txtBlock;

        }

        void txtBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            TextBlock tx = (TextBlock)sender;
            ClassInfo clsitem = (ClassInfo)tx.Tag;
            HtmlPage.Window.Navigate(new Uri("ClassLvlMethodReport.aspx?mod1=" + clsitem.ModuleName + "&nName=" + clsitem.NamespaceName + "&clsName=" + clsitem.Classname + "&rptLink=" + txtTagCaption.Tag.ToString(), UriKind.Relative));
        }


        private LinearGradientBrush GetColorByCovPercentage(ClassInfo clsItem)
        {
            LinearGradientBrush br = new LinearGradientBrush();
            br.StartPoint = new Point(0, 0.5);
            br.EndPoint = new Point(1, 0.5);

            GradientStop gs = new GradientStop();
            gs.Color = Colors.Green;
            gs.Offset = 0.0;
            br.GradientStops.Add(gs);
            GradientStop gs1 = new GradientStop();
            if (clsItem.CovPercentage > 98)
            {
                gs1.Color = Colors.Green;
                gs1.Offset = clsItem.CovPercentage / 100;
            }
            else
            {
                gs1.Color = Colors.Red;
                gs1.Offset = clsItem.CovPercentage / 100;
            }
            br.GradientStops.Add(gs1);

            return br;
        }


    }
}
